package com.onec.service.manage.service.thread;

import com.alibaba.fastjson.JSON;
import com.onec.service.manage.config.ThreadPoolUtil;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import lombok.Data;

public class ThreadTestServiceImpl implements ThreadTestService {

  @Override
  public void test() {

  }


  private static String test(String key, String value) {
    try {
      TimeUnit.SECONDS.sleep(2);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println(Thread.currentThread().getId() + "---key:" + key + "-value:" + value);
    return key + "," + value;
  }

  @Data
  public static class upLoad implements Callable<String> {

    private String key;
    private String value;

    public upLoad(String key, String value) {
      this.key = key;
      this.value = value;
    }

    @Override
    public String call() throws Exception {
      return test(key, value);
    }
  }

//  /**
//   *  主线程还是单线程调用
//   * @param args
//   */
//  public static void main(String[] args) {
//    Map<String, String> requestMap = new HashMap<>(8);
//    requestMap.put("1111", "1111111111");
//    requestMap.put("22222222", "222222222");
//    requestMap.put("33333333", "3333333");
//    requestMap.put("44444", "544444");
//    requestMap.put("5555555", "5555555555");
//
//    //CountDownLatch countDownLatch = new CountDownLatch(requestMap.size());
//    ThreadPoolExecutor threadPoolExecutor = ThreadPoolUtil.getExecutor();
//
//    Map<String, String> resultMap = new HashMap<>(requestMap.size());
//    for (Map.Entry<String, String> entry : requestMap.entrySet()) {
//      Future<String> result = threadPoolExecutor
//          .submit(new upLoad(entry.getKey(), entry.getValue()));
//
//      try {
//        String resutlx = result.get();
//        String[] strings = resutlx.split(",");
//
//        resultMap.put(strings[0], strings[1]);
//      } catch (InterruptedException e) {
//        e.printStackTrace();
//      } catch (ExecutionException e) {
//        e.printStackTrace();
//      }
//    }
//
//    System.out.println(JSON.toJSONString(resultMap));
//  }


  /**
   * 多线程的一个使用场景 批量提交任务并返回结果
   */
  public static void main(String[] args) throws ExecutionException {
    Long startTime = System.currentTimeMillis();
    System.out.println("" + startTime / 1000);
    Map<String, String> requestMap = new HashMap<>(8);
    requestMap.put("1111", "1111111111");
    requestMap.put("22222222", "222222222");
    requestMap.put("33333333", "3333333");
    requestMap.put("44444", "544444");
    requestMap.put("5555555", "5555555555");

    //CountDownLatch countDownLatch = new CountDownLatch(requestMap.size());
    CompletionService<String> service = ThreadPoolUtil.getCompletionService();

    Map<String, String> resultMap = new HashMap<>(requestMap.size());
    int countResult = 0;
    for (Map.Entry<String, String> entry : requestMap.entrySet()) {
      service.submit(new upLoad(entry.getKey(), entry.getValue()));
      countResult = countResult + 1;
    }

    for (int x = 0; x < countResult; x++) {
      try {
        Future<String> stringFuture = service.take();
        String[] strings = stringFuture.get().split(",");
        resultMap.put(strings[0], strings[1]);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
    System.out.println(JSON.toJSONString(resultMap));
    System.out.println("执行时间：" + (System.currentTimeMillis() - startTime) / 1000);
  }
}
